/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.rowan.infovis.soccer;

import com.sun.opengl.util.GLUT;
import javax.media.opengl.GL;

/**
 *
 * @author Kevin McGarry
 */
public class ButtonManager {

    private GLUT glut;

    private StateManager state;

    private float HEIGHT = 347f;
    private float WIDTH = 115f;
    private float PAD = 3f;

    private float LINK_WIDTH = 110f;
    private float LINK_HEIGHT = 40f;

    private float CSV_X = 150;
    private float CSV_Y = 450;

    private boolean onCSV = false;
    boolean csvSelected = false;

    private float PLAYER_X = 750;
    private float PLAYER_Y = 700;

    private boolean onPlayer = false;
    boolean playerSelected = false;

    private float FIELD_X = 475;
    private float FIELD_Y = 700;

    private boolean onField = false;
    boolean fieldSelected = false;

    private float SELECTION_X = 475;
    private float SELECTION_Y = 120;

    private boolean onSelection = false;

    public ButtonManager(StateManager s) {
        this.glut = new GLUT();
        this.state = s;
    }

    public void drawImport(GL gl) {
        
        gl.glPushMatrix();

        gl.glBegin(GL.GL_QUADS);
        gl.glVertex2f(CSV_X + 190, CSV_Y);
        gl.glVertex2f(CSV_X, CSV_Y);
        gl.glVertex2f(CSV_X, 70 + CSV_Y);
        gl.glVertex2f(CSV_X + 190, 70 + CSV_Y);
        gl.glEnd();
        gl.glPopMatrix();
    }

    public void drawFieldViewer(GL gl) {
        setLinkColor(gl,onField);

        gl.glPushMatrix();
        gl.glBegin(GL.GL_QUADS);
        gl.glVertex2f(FIELD_X, FIELD_Y);
        gl.glVertex2f(FIELD_X, LINK_HEIGHT + FIELD_Y);
        gl.glVertex2f(FIELD_X + LINK_WIDTH, LINK_HEIGHT + FIELD_Y);
        gl.glVertex2f(FIELD_X + LINK_WIDTH, FIELD_Y);
        gl.glEnd();
        gl.glPopMatrix();

        gl.glPushMatrix();
        gl.glColor3f(0f, 0f, 0f);
        gl.glRasterPos2f(FIELD_X + 21f, FIELD_Y + 15f);
        glut.glutBitmapString(GLUT.BITMAP_HELVETICA_12, "Field Viewer");
        gl.glPopMatrix();
    }

    public void drawPlayerViewer(GL gl) {
        setLinkColor(gl,onPlayer);

        gl.glPushMatrix();
        gl.glBegin(GL.GL_QUADS);
        gl.glVertex2f(PLAYER_X, PLAYER_Y);
        gl.glVertex2f(PLAYER_X, PLAYER_Y + LINK_HEIGHT);
        gl.glVertex2f(PLAYER_X + LINK_WIDTH, PLAYER_Y + LINK_HEIGHT);
        gl.glVertex2f(PLAYER_X + LINK_WIDTH, PLAYER_Y);
        gl.glEnd();
        gl.glPopMatrix();

        gl.glPushMatrix();
        gl.glColor3f(0f, 0f, 0f);
        gl.glRasterPos2f(PLAYER_X + 21f, PLAYER_Y + 15f);
        glut.glutBitmapString(GLUT.BITMAP_HELVETICA_12, "Player Viewer");
        gl.glPopMatrix();
    }

    void drawSelection(GL gl) {
        setLinkColor(gl,onSelection);

        gl.glPushMatrix();
        gl.glBegin(GL.GL_QUADS);
        gl.glVertex2f(SELECTION_X, SELECTION_Y);
        gl.glVertex2f(SELECTION_X, SELECTION_Y + LINK_HEIGHT);
        gl.glVertex2f(SELECTION_X + LINK_WIDTH, SELECTION_Y + LINK_HEIGHT);
        gl.glVertex2f(SELECTION_X + LINK_WIDTH, SELECTION_Y);
        gl.glEnd();
        gl.glPopMatrix();

        gl.glPushMatrix();
        gl.glColor3f(0f, 0f, 0f);
        gl.glRasterPos2f(SELECTION_X + 2f, SELECTION_Y + 15f);
        if(state.leftSelection()) {
            glut.glutBitmapString(GLUT.BITMAP_HELVETICA_12, "Select Right Player");
        }
        else {
            glut.glutBitmapString(GLUT.BITMAP_HELVETICA_12, "Select Left Player");
        }
        gl.glPopMatrix();
    }

    private void setLinkColor(GL gl,boolean on) {
        if(on) {
            gl.glColor3f(1f, 0f, 0f);
        } else {
            gl.glColor3f(.8f, .8f, .8f);
        }
    }

    private boolean insideField(int x, int y) {
        if (x >= FIELD_X && x <= (FIELD_X + LINK_WIDTH) &&
                y >= FIELD_Y && y <= (FIELD_Y + LINK_HEIGHT)) {
            return true;
        }

        return false;
    }

    private boolean insidePlayer(int x, int y) {
        if (x >= PLAYER_X && x <= (PLAYER_X + LINK_WIDTH) &&
                y >= PLAYER_Y && y <= (PLAYER_Y + LINK_HEIGHT)) {
            return true;
        }

        return false;
    }

    private boolean insideCSV(int x, int y) {
        if (x >= CSV_X && x <= (CSV_X + 190) &&
                y >= CSV_Y && y <= (CSV_Y + 70)) {
            return true;
        }

        return false;
    }

    private boolean insideSelection(int x, int y) {
        if (x >= SELECTION_X && x <= (SELECTION_X + LINK_WIDTH) &&
                y >= SELECTION_Y && y <= (SELECTION_Y + LINK_HEIGHT)) {
            return true;
        }

        return false;
    }

    public void mouseMoved(int x, int y) {
        onSelection = false;
        onCSV = false;
        onPlayer = false;
        onField = false;

        int mouseX = x;
        int mouseY = y ;

        if (state.getState() == 1 && insidePlayer(mouseX, mouseY)) {
            onPlayer = true;
        } else if (state.getState() == 2 && insideField(mouseX, mouseY)) {
            onField = true;
        } else if (state.getState() == 0 && insideCSV(mouseX, mouseY)) {
            onCSV = true;
        } else if (state.getState() == 2 && insideSelection(mouseX, mouseY)) {
            onSelection = true;
        }
    }

    public void mouseReleased(int x, int y) {
        csvSelected = false;
        fieldSelected = false;
        playerSelected = false;

        int mouseX = x;
        int mouseY = y;

        if (state.getState() == 1 && insidePlayer(mouseX, mouseY)) {
            state.setPlayerState();
        } else if (state.getState() == 2 && insideField(mouseX, mouseY)) {
            state.setFieldState();
        } else if (state.getState() == 0 && insideCSV(mouseX, mouseY)) {
            state.setChoosingFile(true);
        } else if (state.getState() == 2 && insideSelection(mouseX, mouseY)) {
            state.swapSelection();
        }
    }

}
